package ms.unicorn.diagram.part;

import java.util.ArrayList;
import java.util.Collections;

import ms.unicorn.language.languagedef.LanguageDef;
import ms.unicorn.language.languagedef.LanguageDefUtil;
import ms.unicorn.language.languagedef.ToolGroup;
import ms.unicorn.language.languagedef.ToolItem;
import ms.unicorn.language.languagedef.XReferenceExpression;
import ms.unicorn.modellanguage.HintedSpecializationType;
import ms.unicorn.modellanguage.IModelLanguage;
import ms.unicorn.modellanguage.LanguageElement;
import ms.unicorn.modellanguage.LanguageEntity;
import ms.unicorn.modellanguage.LanguageRegistry;
import ms.unicorn.palette.LinkToolEntry;
import ms.unicorn.palette.NodeToolEntry;
import ms.unicorn.util.MLExtensionSchema;
import ms.unicorn.util.PluginUtil;

import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.palette.ToolEntry;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.jface.resource.ImageDescriptor;

/**
 * @generated NOT
 */
public class UnicornPaletteFactory implements MLExtensionSchema {
	/**
	 * @param diagramEditor 
	 * @generated NOT
	 */
	public void fillPalette(PaletteRoot paletteRoot, UnicornDiagramEditor diagramEditor) {
		Diagram diagram = diagramEditor.getDiagram();
		ms.unicorn.Diagram domainDiagram = (ms.unicorn.Diagram) diagram.getElement();
		
		// check to filter tool  by domain language		
		ArrayList<IModelLanguage> languages = new ArrayList<IModelLanguage>(LanguageRegistry.INSTANCE.getLanguages(true));
		
		for(IModelLanguage lang: languages) {
			LanguageDef def = lang.getDefinition();
			if (domainDiagram.getLanguage().equals(def.getName())) {
				for (XReferenceExpression groupExp: def.getToolbox().getGroups()) {
					ToolGroup group = LanguageDefUtil.resolveDefinition(groupExp);
					// create group
					PaletteDrawer paletteContainer = new PaletteDrawer(group.getText());
					paletteContainer.setId(group.getName()); 
					//paletteContainer.setDescription(getDescription());
					
					// create tool items
					for (XReferenceExpression itemExp: group.getChildren()) {
						ToolItem item = LanguageDefUtil.resolveDefinition(itemExp);
						LanguageElement e = LanguageRegistry.INSTANCE.getLanguageElement(item.getElement());
						if (e != null) {
							ToolEntry entry = null;
							ImageDescriptor smallImage = PluginUtil.getImageDescriptor(lang.getPluginID(), item.getElement().getSmallImage());
							ImageDescriptor largeImage = PluginUtil.getImageDescriptor(lang.getPluginID(), item.getElement().getLargeImage(), smallImage);
							if (largeImage == null)
								largeImage = smallImage;
							String hint = null;
							if (e instanceof LanguageEntity)
								// create Entity entry
								entry = new NodeToolEntry(item.getText(), item.getTooltip(),
										smallImage, largeImage, hint, Collections.singletonList(e.getElementType()));
							else 
								// create Relation entry
								entry = new LinkToolEntry(item.getText(), item.getTooltip(),
										smallImage, largeImage, hint, Collections.singletonList(e.getElementType()));
							
							if (e.getElementType() instanceof HintedSpecializationType) {
								HintedSpecializationType eType = (HintedSpecializationType) e.getElementType();
								eType.setLanguage(lang);
								eType.setSemanticHint(hint);
							}
							if (entry != null) {
								entry.setToolProperty("language", lang);
								entry.setToolProperty("typeId", item.getName());
								entry.setToolProperty("semanticHint", hint);
								paletteContainer.add(entry);
							}
								
						}
					}
					paletteRoot.add(paletteContainer);
				}
			}
		}
	}
}
